package com.fulu.game.bi.controller.export;


import cn.hutool.crypto.SecureUtil;
import com.fulu.game.bi.AssertHelper;
import com.fulu.game.bi.common.exception.BizException;
import com.fulu.game.bi.entity.dto.ExportDTO;
import com.fulu.game.bi.entity.vo.Result;
import com.fulu.game.bi.service.impl.export.ConsoleExportService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Objects;

@RequestMapping("/api/v1/console")
@RestController
@Slf4j
public class ConsoleExportController {

    @Autowired
    private ConsoleExportService consoleExportService;

    private static final String KEY = "5F432EB68FF63F36045F288E";

    @PostMapping("/export")
    public Result export(@RequestBody ExportDTO dto) {
        log.info("导出参数：{}", new Gson().toJson(dto));
        AssertHelper.assertNotNull(dto.getSign(), () -> new BizException("认证失败"));
        AssertHelper.assertNotNull(dto.getEmail(), () -> new BizException("邮箱为空"));
        try {
            dto.setCondition(dto.getCondition() == null ? null : URLDecoder.decode(dto.getCondition(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("url解码失败", e);
        }
        AssertHelper.assertTrue(Objects.equals(dto.getSign(), SecureUtil.md5(dto.getCondition() + KEY)), () -> new BizException("认证失败"));
        consoleExportService.export(dto);
        return Result.success();
    }

}
